package com.imooc.course.server.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imooc.course.server.dao.CategoryDao;
import com.imooc.course.server.dto.CategoryDto;
import com.imooc.course.server.dto.PageDto;
import com.imooc.course.server.entity.Category;
import com.imooc.course.server.entity.CategoryExample;
import com.imooc.course.server.service.CategoryService;
import com.imooc.course.server.utils.CopyUtil;
import com.imooc.course.server.utils.UuidUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service("categoryService")
public class CategoryServiceImpl implements CategoryService {

    private static final Logger log = LoggerFactory.getLogger(CategoryServiceImpl.class);

    @Resource(name = "categoryDao")
    private CategoryDao categoryDao;


    @Override
    public List<CategoryDto> getAll() {
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.setOrderByClause("sort asc");
        List<Category> categoryList = categoryDao.selectByExample(categoryExample);
        return CopyUtil.copyList(categoryList, CategoryDto.class);
    }

    /**
     * 列表查询
     * @param pageDto
     */
    @Override
    public void getList(PageDto pageDto) {
        PageHelper.startPage(pageDto.getPage(),pageDto.getSize());

        CategoryExample categoryExample = new CategoryExample();
        categoryExample.setOrderByClause("sort asc");
        List<Category> categoryList = categoryDao.selectByExample(categoryExample);

        PageInfo<Category> categoryPageInfo = new PageInfo<>(categoryList);
        pageDto.setTotal(categoryPageInfo.getTotal());

        List<CategoryDto> categoryDtoList = CopyUtil.copyList(categoryList, CategoryDto.class);

        pageDto.setList(categoryDtoList);
    }

    /**
     * 保存，id有值为更新，id无值为新增
     * @param categoryDto
     */
    @Override
    public void save(CategoryDto categoryDto) {
        Category copy = CopyUtil.copy(categoryDto, Category.class);
        String id = categoryDto.getId() != null ? categoryDto.getId().trim() : categoryDto.getId();
        if (StringUtils.isEmpty(id)) {
            this.addCategory(copy);
        }else{
            this.updateCategory(copy);
        }
    }

    private void updateCategory(Category copy) {
        categoryDao.updateByPrimaryKeySelective(copy);
    }

    private void addCategory(Category copy) {
        copy.setId(UuidUtil.getShortUuid());
        categoryDao.insertSelective(copy);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    @Transactional
    public void delete(String id) {
        this.deleteChildren(id);
        categoryDao.deleteByPrimaryKey(id);
    }

    /**
     * 删除子分类
     * @param id
     */
    @Override
    public void deleteChildren(String id) {
        Category category = categoryDao.selectByPrimaryKey(id);
        if ("00000000".equals(category.getParent())) {
            CategoryExample example = new CategoryExample();
            example.createCriteria().andParentEqualTo(category.getId());
            categoryDao.deleteByExample(example);
        }
    }

    @Override
    public List<CategoryDto> getListById(Set<String> categoryIdSet) {
        if (CollectionUtils.isEmpty(categoryIdSet)) {
            return new ArrayList<CategoryDto>();
        }
        CategoryExample example = new CategoryExample();
        CategoryExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(new ArrayList<>(categoryIdSet));
        List<Category> categoryList = categoryDao.selectByExample(example);
        return CopyUtil.copyList(categoryList,CategoryDto.class);
    }
}
